package com.size.check;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Set;

public class CheckerEntry {

    private static final String SAVE_FILE_NAME = "SizeScanResult.txt";

    public static void main(String[] args) {

        if (args == null) return;

        if (args.length == 0) {
            System.err.println("please input  project root path");
            return;
        }
        String rootPath = getWorkDirName(args[0]);

        String[] sizeSmallest = new String[args.length - 1];
        if (args.length <= 1) {
            System.err.println("please input smallest size");
            return;
        }
        for (int i = 1; i < args.length; i++) {
            sizeSmallest[i - 1] = args[i];
        }
        String strSmallestSize = sizeSmallest[0];

        long smallestSize = getSmallesetSize(strSmallestSize);

        if (smallestSize <= 0) {
            System.err.println("smallest is invalid");
            return;
        }

        System.out.println("扫描路径："+rootPath);
        System.out.println("允许最小尺寸："+(smallestSize/1024*1.0f)+" KB");
        long startTime = System.currentTimeMillis();

        deleteResultFile(rootPath);

        Set<SizeModel> fileEntrySet = SizeCheckTask.startCheck(rootPath, smallestSize);
        if (fileEntrySet == null) {
            System.err.println("no files size bigger than " + strSmallestSize);
        }else {
            saveToResultFile(fileEntrySet, rootPath);
        }

        long endTime = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("扫描起始时间："+sdf.format(startTime));
        System.out.println("扫描完成时间："+sdf.format(endTime));
        System.out.println("总共耗时："+((endTime-startTime)/1000.0f)+"秒");
    }

    @NotNull
    private static String getWorkDirName(String arg) {
        String rootPath = arg;

        File f = new File(rootPath);
        rootPath = f.getAbsolutePath();

        if (rootPath.endsWith(File.separator + ".")) {
            rootPath = rootPath.substring(0, rootPath.length() - 2);
        } else if (rootPath.endsWith(File.separator + "..")) {
            rootPath = rootPath.substring(0, rootPath.length() - 3);
            int lastIndex = rootPath.lastIndexOf(File.separator);
            if (lastIndex >= 0) {
                rootPath = rootPath.substring(0, lastIndex);
            }
        }
        return rootPath;
    }

    public static void saveToResultFile(Set<SizeModel> resources, String rootPath) {

        if (resources == null) return;
        File file = new File(rootPath, SAVE_FILE_NAME);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file);
            for (SizeModel resPath : resources) {
                if (resPath == null) continue;
                fos.write(String.format("%s\r\n", resPath).getBytes());
                fos.flush();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("扫描结果保存位置："+file.getAbsolutePath());
        }


    }

    public static void deleteResultFile(String rootPath) {
        File file = new File(rootPath, SAVE_FILE_NAME);
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }

    private static long getSmallesetSize(String strSmallestSize) {
        try {
            return (long) Double.parseDouble(strSmallestSize);
        } catch (Exception e) {
            return 0;
        }
    }


}
